package com.ruoyi.employer.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.employer.domain.CargoUser;
import com.ruoyi.employer.service.ICargoUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 货主用户Controller
 * 
 * @author zhk
 * @date 2024-08-14
 */
@RestController
@RequestMapping("/employer/user")
public class CargoUserController extends BaseController
{
    @Autowired
    private ICargoUserService cargoUserService;

    /**
     * 查询货主用户列表
     */
    @PreAuthorize("@ss.hasPermi('employer:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(CargoUser cargoUser)
    {
        startPage();
        List<CargoUser> list = cargoUserService.selectCargoUserList(cargoUser);
        return getDataTable(list);
    }

    /**
     * 查询所有货主用户列表
     */
    @PreAuthorize("@ss.hasPermi('employer:user:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll()
    {
        List<CargoUser> list = cargoUserService.selectCargoUserList(null);
        return getDataTable(list);
    }

    /**
     * 导出货主用户列表
     */
    @PreAuthorize("@ss.hasPermi('employer:user:export')")
    @Log(title = "货主用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CargoUser cargoUser)
    {
        List<CargoUser> list = cargoUserService.selectCargoUserList(cargoUser);
        ExcelUtil<CargoUser> util = new ExcelUtil<CargoUser>(CargoUser.class);
        util.exportExcel(response, list, "货主用户数据");
    }

    /**
     * 获取货主用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('employer:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cargoUserService.selectCargoUserById(id));
    }

    /**
     * 获取货主用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('employer:user:query')")
    @GetMapping(value = "findByPhone")
    public AjaxResult getInfoByPhone(CargoUser cargoUser) {
        CargoUser user = cargoUserService.selectCargoUserByPhone(cargoUser);
        if (user == null) {
            return error("用户不存在");
        }else {
            return success(user);
        }
    }

    /**
     * 新增货主用户
     */
    @PreAuthorize("@ss.hasPermi('employer:user:add')")
    @Log(title = "货主用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CargoUser cargoUser)
    {
        return toAjax(cargoUserService.insertCargoUser(cargoUser));
    }

    /**
     * 修改货主用户
     */
    @PreAuthorize("@ss.hasPermi('employer:user:edit')")
    @Log(title = "货主用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CargoUser cargoUser)
    {
        return toAjax(cargoUserService.updateCargoUser(cargoUser));
    }

    /**
     * 删除货主用户
     */
    @PreAuthorize("@ss.hasPermi('employer:user:remove')")
    @Log(title = "货主用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cargoUserService.deleteCargoUserByIds(ids));
    }
}
